package com.ysxsoft.wdg.ui.activity.my;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.response.DaiLiStatusResponse;
import com.ysxsoft.wdg.bean.response.XieYiResponse;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.net.UrlHost;
import com.ysxsoft.wdg.util.JsonUtils;
import com.ysxsoft.wdg.util.WebViewUtils;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;
import com.ysxsoft.wdg.util.statusbar.StatusBarUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class ShopRz1Activity extends BaseActivity {

    public static void start(Context context) {
        Intent intent = new Intent(context, ShopRz1Activity.class);
        context.startActivity(intent);
    }

    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.iv_pic)
    ImageView ivPic;
    @BindView(R.id.tv_rule)
    TextView tvRule;
    @BindView(R.id.xieyi)
    TextView xieyi;
    @BindView(R.id.checkBox)
    CheckBox checkBox;
    @BindView(R.id.btn_sure)
    Button btnSure;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_shop_rz1;
    }

    @Override
    protected void initData() {
        StatusBarUtil.setStatusBarTextColor(this, true);
        initStatusBar(topView, true);
        titleContent.setText("商家入驻");

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (toStatus) {
            getStatus();
        }
    }

    @Override
    protected void setListener() {

    }

    @OnClick({R.id.title_finish, R.id.btn_sure, R.id.xieyi})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_finish:
                finish();
                break;
            case R.id.btn_sure:
                if (canClick) {
                    ShopRzInputActivity.start(mContext);
                }
                break;
            case R.id.xieyi:
                Intent intent = new Intent(this, XieYiActivity.class);
                startActivity(intent);
                break;
        }
    }

    /**
     * 获取区域代理状态
     */
    private void getStatus() {
        OkHttpUtils.post()
                .url(UrlHost.SHANG_STATUS)
                .addParams("uid", SharePrefUtils.getUserId())
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

                if (!call.isCanceled()) {
//                    multStatusView.showContent();
                    canClick = true;
                }
            }

            @Override
            public void onResponse(String response, int id) {
                canClick = true;
                if (response != null) {
                    DaiLiStatusResponse daiLiStatusResponse = JsonUtils.parseByGson(response, DaiLiStatusResponse.class);
                    if (daiLiStatusResponse != null) {
                        if (CallbackCode.SUCCESS.equals(daiLiStatusResponse.getCode())) {
                            int data = daiLiStatusResponse.getData();//  1未申请，2审核中，3已审核
                            if (data == 1) {
                                isFirst = false;
                                btnSure.setText("成为商家");
                                btnSure.setVisibility(View.VISIBLE);
                                checkBox.setVisibility(View.VISIBLE);

                                checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                                    @Override
                                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                        btnSure.setEnabled(isChecked);
                                    }
                                });
                            } else if (data == 2) {
                                btnSure.setText("审核中");
                                btnSure.setVisibility(View.VISIBLE);
                                canClick = false;
                                isFirst = false;
                            } else if (data == 3) {
                                btnSure.setText("已审核");
                                btnSure.setVisibility(View.VISIBLE);
                                canClick = false;
                                btnSure.setEnabled(true);
                                toStatus = false;
                                if (isFirst) {
                                    Intent intent = new Intent(ShopRz1Activity.this, MyShopActivity.class);
                                    startActivity(intent);
                                    finish();
                                }
                            }
                        }
                    }
                } else {
//                    multStatusView.showError();
                }
            }
        });
    }


    private boolean isFirst = true;
    private boolean canClick = true;
    private boolean toStatus = true;
}
